package service;

import org.junit.jupiter.api.Test;

import java.math.BigDecimal;
import java.util.ArrayList;


public class Client {
    static BigDecimal begin = BigDecimal.valueOf(0);
    public static void main(String[] args) {

           //1 用户购买产品 : 单价，数量，单品折扣
        ArrayList<Fruit> custA = new ArrayList<>();
        custA.add(new AppleFruit(811.111,1,0.85));

        //2 选择营销策略
        StrategyFactory.init();

        Strategy strategy = StrategyFactory.getConcretStrategy(1);

        //3 得到最后结果
        BigDecimal end = execute(custA,strategy);
        System.out.println(end.equals(new BigDecimal("689.4")));
    }

    public static BigDecimal execute(ArrayList<Fruit> custA,Strategy strategy) {
        begin= new BigDecimal("0");
        custA.forEach(x->{
            begin = begin.add(x.calculateEach());
        });
        // 原始总价 + 策略
        Context context = new Context(strategy, begin);
        // 策略执行
        return context.calcforSum();// 可以处理先后
    }

    @Test
    public void test(){
        custA();
        custB();
        custC();
        custD();
    }

    @Test
    public void custA(){
        //1 用户选产品 : 单价，数量，单品折扣
        ArrayList<Fruit> custA = new ArrayList<>();
        custA.add(new AppleFruit(8,3,1));
        custA.add(new AppleFruit(13,1,1));
        //2 选择营销策略
        StrategyFactory.init();
        Strategy strategy = StrategyFactory.getConcretStrategy(2);

        //3 得到最后结果
        BigDecimal end = execute(custA,strategy);
        System.out.println(end.equals(new BigDecimal("37.0")));
    }


    @Test
    public void custB(){
        //1 用户选产品 : 单价，数量，单品折扣
        ArrayList<Fruit> custB = new ArrayList<>();
        custB.add(new AppleFruit(8,3,1));
        custB.add(new StrawberryFruit(13,1,1));
        custB.add(new MangoFruit(20,1,1));
        //2 选择营销策略
        StrategyFactory.init();
        Strategy strategy = StrategyFactory.getConcretStrategy(1);

        //3 得到最后结果
        BigDecimal end = execute(custB,strategy);
        System.out.println(end.equals(new BigDecimal("57.0")));
    }

    @Test
    public void custC(){
        //1 用户选产品 : 单价，数量，单品折扣
        ArrayList<Fruit> custB = new ArrayList<>();
        custB.add(new AppleFruit(8,3,1));
        custB.add(new StrawberryFruit(13,1,1));
        custB.add(new StrawberryFruit(20,5,0.8));
        //2 选择营销策略
        StrategyFactory.init();
        Strategy strategy = StrategyFactory.getConcretStrategy(1);

        //3 得到最后结果
        BigDecimal end = execute(custB,strategy);
        //System.out.println(end.equals(new BigDecimal("137.0")));
        System.out.println(end.equals(new BigDecimal("117.0")));
    }

    @Test
    public void custD(){
        //1 用户选产品 : 单价，数量，单品折扣
        ArrayList<Fruit> custD = new ArrayList<>();
        custD.add(new AppleFruit(8,3,1));
        custD.add(new StrawberryFruit(13,1,1));
        custD.add(new StrawberryFruit(20,5,0.8));
        //2 选择营销策略
        StrategyFactory.init();
        Strategy strategy = StrategyFactory.getConcretStrategy(2);

        //3 得到最后结果
        BigDecimal end = execute(custD,strategy);
        //System.out.println(end.equals(new BigDecimal("137.0")));
        System.out.println(end.equals(new BigDecimal("107.0")));
    }
}
